package com.canyou.controller;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import com.canyou.common.CanYouController;
import com.canyou.model.Area;
import com.canyou.model.Employee;
import com.canyou.model.Param;
import com.canyou.util.FileUtil;
import com.canyou.util.PageHelper;
import com.jfinal.log.Logger;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.upload.UploadFile;

/**
 * 员工管理
 * 
 * @created cgz 2012-12-15
 */
public class EmployeeController extends CanYouController {
	private static Logger logger = Logger.getLogger(EmployeeController.class);

	public void index() throws UnsupportedEncodingException {
		int skillRank = getParaToInt("skillRank", -1);
		String employeeName = getPara("employeeName", "");
		int index = getParaToInt("index", 1);

		Page<Employee> page = Employee.dao.search(index, skillRank,
				employeeName);
		if (null != page) {
			List<Employee> employes = page.getList();
			List<Employee> list = new ArrayList<Employee>();
			int size = employes.size();
			for (int i = 0; i < size; i++) {
				Employee employee = employes.get(i);
				int sex = employee.get("Sex", 0);
				employee.set("Sex", sex == 0 ? "男" : "女");
				int skillrank = employee.get("SkillRank", -1);
				employee.set("SkillRank", Param.dao.getTextById(skillrank));
				list.add(employee);
			}
			String path = "/employee/index?skillRank=" + skillRank
					+ "&employeeName=" + employeeName + "&index=";
			setAttr("pager", new PageHelper(page, path));
			setAttr("employees", list);
			setAttr("returnUrl", URLEncoder.encode(path + index, "UTF-8"));// 返回地址
		}
		setAttr("skillranks", Param.dao.getListByType(4));

		keepPara();// 保留参数值
		totast().render("/employee/list.jsp");
	}

	public void delete() throws UnsupportedEncodingException {
		boolean result = false;
		try {
			result = Employee.dao.deleteById(getPara("id"));
		} catch (ActiveRecordException e) {
			logger.error(e.getMessage(), e);
		}
		setSessionAttr("toast", result ? "删除成功！" : "删除失败！");
		redirect(URLDecoder.decode(getPara("returnUrl", ""), "UTF-8"), true);

	}

	public void add() {
		form();
	}

	public void edit() throws UnsupportedEncodingException {
		Employee employee = null;
		try {
			employee = Employee.dao.findById(getPara("id", ""));
		} catch (ActiveRecordException e) {
			logger.error(e.getMessage(), e);
		}
		if (null != employee) {
			setAttr("employee", employee);
			int province = employee.get("Province", -1);
			setAttr("citys", Area.dao.getListById(province));
			int city = employee.get("City", -1);
			setAttr("districts", Area.dao.getListById(city));
			form();
		} else {
			setSessionAttr("toast", "查看失败，无法编辑，请稍候再试！");
			redirect(URLDecoder.decode(getPara("returnUrl", ""), "UTF-8"), true);// redirect(url)默认是带上参数的
		}
	}

	private void form() {
		setAttr("provinces", Area.dao.getListById(0));
		setAttr("educations", Param.dao.getListByType(4));
		setAttr("skillRanks", Param.dao.getListByType(2));
		setAttr("parentTabTitle", getPara("parentTabTitle"));
		totast().render("/employee/form.jsp");
	}

	public void update() throws UnsupportedEncodingException {
		boolean result = false;
		Employee employee = getModel(Employee.class, "employee");
		UploadFile uploadFile = getFile();
		if (uploadFile != null) {
			File file = uploadFile.getFile();
			String path = UUID.randomUUID().toString()
					+ FileUtil.getExt(uploadFile.getFileName());
			if (file.renameTo(new File(uploadFile.getSaveDirectory() + path))) {
				employee.set("PhotoUrl", path);
			}
			try {
				result = employee.update(getSessionAttr("UserName").toString());
			} catch (ActiveRecordException e) {
				logger.error(e.getMessage(), e);
			}
		}
		setSessionAttr("toast", result ? "新增成功！" : "新增失败！");
		index();
	}

	public void save() throws UnsupportedEncodingException {
		boolean result = false;
		Employee employee = getModel(Employee.class, "employee");
		UploadFile uploadFile = getFile();
		if (uploadFile != null) {
			File file = uploadFile.getFile();
			String path = UUID.randomUUID().toString()
					+ FileUtil.getExt(uploadFile.getFileName());
			if (file.renameTo(new File(uploadFile.getSaveDirectory() + path))) {
				employee.set("PhotoUrl", path);
			}
			try {
				result = employee.save(getSessionAttr("UserName").toString());
			} catch (ActiveRecordException e) {
				logger.error(e.getMessage(), e);
			}
		}
		setSessionAttr("toast", result ? "新增成功！" : "新增失败！");
		index();
	}
}
